package com.example.webapi.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.webapi.Utils.ResponseModel;
import com.example.webapi.Utils.WriteJson;
import com.example.webapi.entity.Menu;
import com.example.webapi.service.ListdataService;
import com.example.webapi.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author  张彬
 * @since 2021-10-27
 */
@RestController
@RequestMapping("/api")
public class MenuController {
    @Autowired
    MenuService menuService;
    @Autowired
    ListdataService listdataService;
    /**
     * 获取所有菜单id
     * @return
     */
    @GetMapping("getMenu")
    public ResponseModel getMenu(){
        //第一次请求树结构文件先写入json
        WriteJson.wrtie(listdataService);

        List<Menu> menuList = menuService.list();
        return ResponseModel.success(menuList);
    }

    @PostMapping("updateRuleData")
    public ResponseModel updateRuleInfo(@RequestBody JSONObject data){
        Map<String,Object> map = menuService.getMap(new UpdateWrapper<Menu>().eq("tableId",data.getInt("tableId")));
        Menu menu = new Menu();
        menu.setId(Convert.toInt(map.get("parentId")));
        menu.setRuleName(data.getStr("ruleName"));
        menuService.updateById(menu);
        boolean result = menuService.update(null,new UpdateWrapper<Menu>().eq("tableId",data.getInt("tableId"))
                .set("ruleInfo",data.getStr("ruleInfo")));
        if (result){
            return ResponseModel.success("修改成功");
        }else{
            return ResponseModel.fail("修改失败");
        }
    }

    @GetMapping("getRuleData")
    public ResponseModel getRuleData(@RequestParam Integer id){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> menuMap = menuService.getMap(new QueryWrapper<Menu>().eq("tableId",id));
        map.put("ruleInfo",menuMap.get("ruleInfo"));
        Menu menu = menuService.getById(Convert.toInt(menuMap.get("parentId")));
        map.put("ruleName",menu.getRuleName());
//        Map<String,Object> menu = menuService.getMap(new QueryWrapper<Menu>().select("ruleName","ruleInfo").eq("tableId",id));
        return ResponseModel.success(map);
    }


    @GetMapping("getMenuData")
    public ResponseModel getMenuData(@RequestParam Integer id){
        Menu menuMap = menuService.getById(id);
        return ResponseModel.success(menuMap);
    }


    @PostMapping("updateMenuData")
    public ResponseModel updateMenuData(@RequestBody JSONObject data){
        Menu menu = new Menu();
        menu.setId(data.getInt("id"));
        menu.setRuleInfo(data.getStr("ruleInfo"));
        menu.setRuleName(data.getStr("ruleName"));
        Boolean result = menuService.updateById(menu);
        if (result){
            return ResponseModel.success("修改成功");
        }else{
            return ResponseModel.fail("修改失败");
        }
    }
}
